from django.shortcuts import render

# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from rest_framework.generics import RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from rest_framework.viewsets import ReadOnlyModelViewSet

from goods import serializers
from goods.models import Goods, GoodsCategory, GoodsAlbum
from goods.serializers import GoodsSerializer, SubGoodsCategorySerializer, GoodsCategorySerializer


# URL:/goods/?Category=category_id&ordering=-create_time
# class GoodsView(APIView):
#     def get(self, request):
#         goods = request.GET.get("Category")


class GoodsViewSet(ReadOnlyModelViewSet):
    """
    1. 商品列表界面: 查询所有的商品(过滤一个类别下的所有商品)
       http://127.0.0.1:8000/goods/?ordering=-bread
    2. 商品详情界面: 查询一个商品的详细信息(后面用到)
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer

    # 指定过滤后台和排序字段
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filter_fields = ('category',)
    ordering_fields = ('sell_price', 'create_time', 'sales')


class CategoryDetailView(RetrieveAPIView):
    """
    查询一个类别信息[面包屑导航]
    """
    serializer_class = GoodsCategorySerializer
    queryset = GoodsCategory.objects.all()


class GoodsRecommendView(APIView):

    # /goods/recommend/
    def get(self, request):
        """
        首页: 获取商品首页右侧的推荐商品
        """
        goods = Goods.objects.filter(is_red=1).order_by('-create_time')[0:4]
        serializer = GoodsSerializer(goods, many=True)
        return Response(serializer.data)


class CategoryGoodsView(APIView):

    # /goods/category/
    def get(self, request):
        """首页: 获取首页的类别商品"""

        list_data = []
        # 获取一级大类别
        categories = GoodsCategory.objects.filter(parent_id=0)
        # 遍历每一个大类别
        for category in categories:
            dict_category = GoodsCategorySerializer(category).data
            child_category_set = category.goodscategory_set.all()

            # 获取所有的子类别id
            child_category_ids = []
            for category in child_category_set:
                child_category_ids.append(category.id)

            # 查询大类别中的显示在首页的5个商品
            query_set = Goods.objects.filter(category_id__in=child_category_ids) \
                           .order_by('-create_time')[0:5]
            goods_list = GoodsSerializer(query_set, many=True).data
            dict_category['goods'] = goods_list

            list_data.append(dict_category)

        return Response(list_data)


# # 以下代码来自组长
# class ParticularsGoodsAPIView(APIView):
#     def get(self, request, goods_id):
#         goods = Goods.objects.get(id=goods_id)
#         ParticularsGoodData = ParticularsGoodsSerializer(goods).data
#         # 使用关联对象序列化，此处不再使用
#         # image = GoodsAlbum.objects.filter(goods_id = goods_id)
#         # image_data = ImageGoodsSerializer(image,many=True).data
#         # ParticularsGoodData['goodsalbum_set'] = image_data
#         # Particulars = GoodsAlbum.objects.filter(goods = goods)
#         # ParticularsGoodData['goodsalbum_set'] = ImageGoodsSerializer(Particulars,many=True)
#         return Response(ParticularsGoodData)
#
#
# class RecommandGoodsAPIView(APIView):
#     def get(self, request):
#         goods = Goods.objects.filter(is_red = 1).order_by('-create_time')[0:4]
#         ParticularsGoodData = RecommandGoodsSerializer(goods,many=True).data
#         return Response(ParticularsGoodData)
